// part1:C12:ex12.15.cpp
// shared_ptr and connction.
#include <iostream>
#include <memory>
using namespace std;

struct desination {};
struct connection {};

connection connect(desination *pd)
{
  cout << "open connection" << endl;
  return connection();
}
void disconnect(connection c)
{
  cout << "close connection" << endl;
}

// unused shared_ptr method
void f(desination &d)
{
  cout << "directed manage connect" << endl;
  connection c = connect(&d);
  cout << endl;
}

// use shared_ptr method
void f1(desination &d)
{
  cout << "use shared_ptr manege connect" << endl;
  connection c = connect(&d);

  shared_ptr<connection> p(&c,
                           [](connection *p){
                             disconnect(*p);
                             // forget call disconnect method to close connction.
                           });
}

int main(int argc, char *argv[])
{
  desination d;
  f(d);
  f1(d);

  return 0;
}
